/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import edu.gatech.jiggawatt.office.model.Doctor;
import edu.gatech.jiggawatt.office.model.Nurse;
import edu.gatech.jiggawatt.office.model.Patient;
import edu.gatech.jiggawatt.office.model.TreatmentRecord;
import javax.swing.JList;
import javax.swing.JTabbedPane;
import edu.gatech.jiggawatt.office.presenter.Presenter;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JRadioButton;

/**
 * Represents Admin Screen
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class AdminFrame extends JFrame { // $codepro.audit.disable declareDefaultConstructors

	private Presenter presenter;
	private JTextField idField;
	private JTextField idField1;
	private JTextField idField2;
	private JTextField fNameField;
	private JTextField fNameField1;
	private JTextField fNameField2;
	private JTextField lNameField;
	private JTextField lNameField1;
	private JTextField lNameField2;
	private JTextField ageField;
	private JTextField ageField1;
	private JTextField ageField2;
	private JTextField phoneField;
	private JTextField phoneField1;
	private JTextField phoneField2;
	private JTextField emailField;
	private JTextField emailField1;
	private JTextField emailField2;
	private JTextArea addressArea;
	private JTextArea addressArea1;
	private JTextArea addressArea2;
	private JList patientList;
	private JList nurseList;
	private JList doctorList;
	private JRadioButton rdbtnMale;
	private JRadioButton rdbtnMale1;
	private JRadioButton rdbtnMale2;
	private JRadioButton rdbtnFemale;
	private JRadioButton rdbtnFemale1;
	private JRadioButton rdbtnFemale2;	
	private DefaultListModel patientModel;
	private DefaultListModel nurseModel;
	private DefaultListModel doctorModel;
	private DefaultListModel trModel;
	private JButton btnSetPassword;
	private JButton btnSetPassword1;
	private JButton btnSetPassword2;
	private JButton btnShowMessages1;
	private JButton btnShowMessages2;
	private JButton btnAddPatient;
	private JButton btnAddNurse;
	private JButton btnAddDoctor;
	private JButton btnRemovePatient;
	private JButton btnRemoveNurse;
	private JButton btnRemoveDoctor;
	private JButton btnUpdatePatient;
	private JButton btnUpdateNurse;
	private JButton btnUpdateDoctor;
	private JRadioButton rdbtnYes;
	private JRadioButton rdbtnYes1;
	private JRadioButton rdbtnYes2;
	private JRadioButton rdbtnNo;
	private JRadioButton rdbtnNo1;
	private JRadioButton rdbtnNo2;
	private ButtonGroup genderGroup;
	private ButtonGroup genderGroup1;
	private ButtonGroup genderGroup2;
	private ButtonGroup suspendedGroup;
	private ButtonGroup suspendedGroup1;
	private ButtonGroup suspendedGroup2;
	private JLabel lblPharmacy;
	private JLabel lblInsurance;
	private JLabel lblAllergy;
	private JTextField pharmacyField;
	private JTextField insuranceField;
	private JScrollPane scrollPane;
	private JList TRlist; // $codepro.audit.disable instanceFieldNamingConvention
	private JLabel lblTreatmentRecords;
	private JButton btnViewEditTreatmentRecords;
	private JScrollPane scrollPane_2;
	private JTextArea allergyArea;	
	
	/**
	 * Create the admin frame.
	 * @param p presenter
	 * @param id id of admin
	 */
	public AdminFrame(Presenter p, String id) 
	{
		setTitle("Awesome Office"); // $codepro.audit.disable stringLiterals
		presenter = p;
		
		setBounds(100, 100, 800, 600); // $codepro.audit.disable numericLiterals
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		setContentPane(panel);

		JLabel lbladminScreen = new JLabel("Admin logged in as : " + id); // $codepro.audit.disable stringLiterals
		lbladminScreen.setBounds(10, 11, 300, 25); // $codepro.audit.disable numericLiterals
		panel.add(lbladminScreen);
				
		JButton btnLogout = new JButton("Logout"); // $codepro.audit.disable stringLiterals
		btnLogout.setBounds(190, 12, 120, 23); // $codepro.audit.disable numericLiterals
		panel.add(btnLogout);
		
		//Logout handler
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				setVisible(false);
				dispose();
				JFrame loginFrame = new LoginFrame(presenter);
				loginFrame.setVisible(true);
			}
		});
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 45, 784, 517); // $codepro.audit.disable numericLiterals
		panel.add(tabbedPane);
		
		JPanel patients_panel = new JPanel();
		tabbedPane.addTab("Patients", null, patients_panel, null); // $codepro.audit.disable stringLiterals
		patients_panel.setLayout(null);
		
		JLabel lblId = new JLabel("ID:"); // $codepro.audit.disable stringLiterals
		lblId.setBounds(264, 12, 69, 14); // $codepro.audit.disable numericLiterals
		patients_panel.add(lblId);
		
		idField = new JTextField();
		idField.setEditable(false);
		idField.setBounds(343, 12, 114, 20); // $codepro.audit.disable numericLiterals
		patients_panel.add(idField);
		idField.setColumns(10); // $codepro.audit.disable numericLiterals
		
		JLabel lblPw = new JLabel("PW:"); // $codepro.audit.disable stringLiterals
		lblPw.setBounds(264, 43, 69, 14); // $codepro.audit.disable numericLiterals
		patients_panel.add(lblPw);
		
		JLabel lblFirstName = new JLabel("First Name:"); // $codepro.audit.disable stringLiterals
		lblFirstName.setBounds(264, 74, 69, 14); // $codepro.audit.disable numericLiterals
		patients_panel.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name:"); // $codepro.audit.disable stringLiterals
		lblLastName.setBounds(264, 105, 69, 14); // $codepro.audit.disable numericLiterals
		patients_panel.add(lblLastName);
		
		JLabel lblAge = new JLabel("Age:"); // $codepro.audit.disable stringLiterals
		lblAge.setBounds(264, 136, 69, 14); // $codepro.audit.disable numericLiterals
		patients_panel.add(lblAge);
		
		JLabel lblGender = new JLabel("Gender:"); // $codepro.audit.disable stringLiterals
		lblGender.setBounds(264, 169, 69, 14); // $codepro.audit.disable numericLiterals
		patients_panel.add(lblGender);
		
		fNameField = new JTextField();
		fNameField.setEditable(false);
		fNameField.setBounds(343, 74, 114, 20);
		patients_panel.add(fNameField);
		fNameField.setColumns(10);
		
		lNameField = new JTextField();
		lNameField.setEditable(false);
		lNameField.setBounds(343, 105, 114, 20);
		patients_panel.add(lNameField);
		lNameField.setColumns(10);
		
		ageField = new JTextField();
		ageField.setEditable(false);
		ageField.setBounds(343, 136, 114, 20);
		patients_panel.add(ageField);
		ageField.setColumns(10);
		
		JLabel lblAddress = new JLabel("Address:"); // $codepro.audit.disable stringLiterals
		lblAddress.setBounds(264, 198, 69, 14);
		patients_panel.add(lblAddress);
		
		JLabel lblPhone = new JLabel("Phone:"); // $codepro.audit.disable stringLiterals
		lblPhone.setBounds(264, 259, 46, 14);
		patients_panel.add(lblPhone);
		
		phoneField = new JTextField();
		phoneField.setEditable(false);
		phoneField.setBounds(343, 259, 114, 20);
		patients_panel.add(phoneField);
		phoneField.setColumns(10);
		
		JLabel lblEmail = new JLabel("E-mail:"); // $codepro.audit.disable stringLiterals
		lblEmail.setBounds(264, 290, 46, 14);
		patients_panel.add(lblEmail);
		
		emailField = new JTextField();
		emailField.setEditable(false);
		emailField.setBounds(343, 290, 114, 20);
		patients_panel.add(emailField);
		emailField.setColumns(10);
		
		JLabel lblSuspended = new JLabel("Suspended:"); // $codepro.audit.disable stringLiterals
		lblSuspended.setBounds(264, 414, 68, 14);
		patients_panel.add(lblSuspended);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 11, 234, 398);
		patients_panel.add(scrollPane_1);
		
		//initialize patient list
		patientModel = new DefaultListModel();
		for (Patient e : presenter.getPatientList())
		{
			patientModel.addElement(e);
		}
		
		patientList = new JList(patientModel);
		patientList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane_1.setViewportView(patientList);
		
		btnSetPassword = new JButton("Set Password"); // $codepro.audit.disable stringLiterals
		btnSetPassword.setEnabled(false);
		btnSetPassword.setBounds(343, 43, 114, 20);
		patients_panel.add(btnSetPassword);
		
		btnAddPatient = new JButton("Add Patient"); // $codepro.audit.disable stringLiterals
		btnAddPatient.setBounds(10, 430, 107, 27);
		patients_panel.add(btnAddPatient);
		
		btnRemovePatient = new JButton("Remove Patient"); // $codepro.audit.disable stringLiterals
		btnRemovePatient.setEnabled(false);
		btnRemovePatient.setBounds(121, 430, 123, 27);
		patients_panel.add(btnRemovePatient);
		
		btnUpdatePatient = new JButton("Update Patient"); // $codepro.audit.disable stringLiterals
		btnUpdatePatient.setEnabled(false);
		btnUpdatePatient.setBounds(343, 440, 114, 27);
		patients_panel.add(btnUpdatePatient);
		
		addressArea = new JTextArea();
		addressArea.setEditable(false);
		addressArea.setBounds(343, 198, 114, 50);
		patients_panel.add(addressArea);
		
		rdbtnMale = new JRadioButton("Male"); // $codepro.audit.disable stringLiterals
		rdbtnMale.setEnabled(false);
		rdbtnMale.setBounds(343, 167, 53, 20);
		patients_panel.add(rdbtnMale);
		
		rdbtnFemale = new JRadioButton("Female"); // $codepro.audit.disable stringLiterals
		rdbtnFemale.setEnabled(false);
		rdbtnFemale.setBounds(396, 167, 78, 20);
		patients_panel.add(rdbtnFemale);
		
		genderGroup = new ButtonGroup();
		genderGroup.add(rdbtnMale);
		genderGroup.add(rdbtnFemale);
		
		rdbtnYes = new JRadioButton("Yes"); // $codepro.audit.disable stringLiterals
		rdbtnYes.setEnabled(false);
		rdbtnYes.setBounds(343, 411, 53, 20);
		patients_panel.add(rdbtnYes);
		
		rdbtnNo = new JRadioButton("No");
		rdbtnNo.setEnabled(false);
		rdbtnNo.setBounds(404, 411, 53, 20);
		patients_panel.add(rdbtnNo);
		
		suspendedGroup = new ButtonGroup();
		suspendedGroup.add(rdbtnYes);
		suspendedGroup.add(rdbtnNo);
		
		lblPharmacy = new JLabel("Pharmacy:"); // $codepro.audit.disable stringLiterals
		lblPharmacy.setBounds(264, 321, 61, 14);
		patients_panel.add(lblPharmacy);
		
		lblInsurance = new JLabel("Insurance:"); // $codepro.audit.disable stringLiterals
		lblInsurance.setBounds(264, 346, 69, 14);
		patients_panel.add(lblInsurance);
		
		lblAllergy = new JLabel("Allergy:"); // $codepro.audit.disable stringLiterals
		lblAllergy.setBounds(264, 370, 69, 14);
		patients_panel.add(lblAllergy);
		
		pharmacyField = new JTextField();
		pharmacyField.setEditable(false);
		pharmacyField.setBounds(343, 318, 114, 20);
		patients_panel.add(pharmacyField);
		pharmacyField.setColumns(10);
		
		insuranceField = new JTextField();
		insuranceField.setEditable(false);
		insuranceField.setBounds(343, 343, 114, 20);
		patients_panel.add(insuranceField);
		insuranceField.setColumns(10);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(495, 44, 228, 365);
		patients_panel.add(scrollPane);
		
		trModel = new DefaultListModel();
		TRlist = new JList(trModel);
		scrollPane.setViewportView(TRlist);
		
		lblTreatmentRecords = new JLabel("Treatment Records:");
		lblTreatmentRecords.setBounds(495, 18, 114, 14);
		patients_panel.add(lblTreatmentRecords);
		
		btnViewEditTreatmentRecords = new JButton("View/Edit Treatment Records");
		btnViewEditTreatmentRecords.setEnabled(false);
		btnViewEditTreatmentRecords.setBounds(495, 432, 228, 23);
		patients_panel.add(btnViewEditTreatmentRecords);
		
		scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(343, 370, 114, 39);
		patients_panel.add(scrollPane_2);
		
		allergyArea = new JTextArea();
		allergyArea.setEditable(false);
		scrollPane_2.setViewportView(allergyArea);
		
		//Sets up patient list handler when patient list is clicked
		patientList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				//When patient list is unfocused, delete all the current entries
				if (patientList.getSelectedIndex() == -1)
				{
					idField.setText("");
					fNameField.setText("");
				    lNameField.setText("");	
				    ageField.setText("");
				    rdbtnMale.setSelected(false);
				    rdbtnFemale.setSelected(false);
				    genderGroup.clearSelection();
				    suspendedGroup.clearSelection();
				    addressArea.setText("");
				    phoneField.setText("");
				    emailField.setText("");
				    pharmacyField.setText("");
				    insuranceField.setText("");
				    allergyArea.setText("");
				    idField.setEditable(false);
					fNameField.setEditable(false);
				    lNameField.setEditable(false);
				    ageField.setEditable(false);
				    				    
				    addressArea.setEditable(false);
				    phoneField.setEditable(false);
				    emailField.setEditable(false);
				    
				    pharmacyField.setEditable(false);
				    insuranceField.setEditable(false);
				    allergyArea.setEditable(false);
				    
				    rdbtnYes.setEnabled(false);
				    rdbtnNo.setEnabled(false);
				    rdbtnMale.setEnabled(false);
				    rdbtnFemale.setEnabled(false);
			    
				    btnSetPassword.setEnabled(false);
				    btnRemovePatient.setEnabled(false);
				    btnUpdatePatient.setEnabled(false);
				    
				    trModel.clear();  
				}
				// Updates fields according to the patient clicked
				else
				{
					idField.setText((((Patient) patientList.getSelectedValue()).getId()));
					fNameField.setText((((Patient) patientList.getSelectedValue()).getFirstName()));
				    lNameField.setText((((Patient) patientList.getSelectedValue()).getLastName()));
				    ageField.setText(Integer.toString(((Patient) patientList.getSelectedValue()).getAge()));
				    if (((Patient) patientList.getSelectedValue()).getGender() == 0)
				    {
				    	rdbtnMale.setSelected(true);
				    }
				    else
				    {
				    	rdbtnFemale.setSelected(true);
				    }
				    
				    addressArea.setText((((Patient) patientList.getSelectedValue()).getAddress()));
				    phoneField.setText((((Patient) patientList.getSelectedValue()).getPhone()));
				    emailField.setText((((Patient) patientList.getSelectedValue()).getEmail()));
				    
				    pharmacyField.setText((((Patient) patientList.getSelectedValue()).getPharmacy()));
				    insuranceField.setText((((Patient) patientList.getSelectedValue()).getInsurance()));
				    allergyArea.setText((((Patient) patientList.getSelectedValue()).getAllergies()));
				    
				    if (((Patient) patientList.getSelectedValue()).isSuspended())
				    {
				    	rdbtnYes.setSelected(true);
				    }
				    else
				    {
				    	rdbtnNo.setSelected(true);
				    }
				    
					fNameField.setEditable(true);
				    lNameField.setEditable(true);
				    ageField.setEditable(true);
				    addressArea.setEditable(true);
				    phoneField.setEditable(true);
				    emailField.setEditable(true);
				    rdbtnYes.setEnabled(true);
				    rdbtnNo.setEnabled(true);
				    
				    pharmacyField.setEditable(true);
				    insuranceField.setEditable(true);
				    allergyArea.setEditable(true);

				    rdbtnMale.setEnabled(true);
				    rdbtnFemale.setEnabled(true);
				    btnSetPassword.setEnabled(true);
				    btnRemovePatient.setEnabled(true);
				    btnUpdatePatient.setEnabled(true);
				    
				    trModel.clear(); 
				    for (TreatmentRecord tr : presenter.getTreatmentRecords(((Patient) patientList.getSelectedValue()).getId()))
				    {
				    	trModel.addElement(tr);
				    }		
				}
			}
		});
		
		//Update patient button handler
		btnUpdatePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					int gender;
					if (rdbtnMale.isSelected())
					{
						gender = 0;
					}
					else
					{
						gender = 1;
					}
					Patient updatedPatient = new Patient(idField.getText(), null, fNameField.getText(), lNameField.getText(), 
							Integer.parseInt(ageField.getText()), gender, addressArea.getText(),
							phoneField.getText(), emailField.getText());
					
				    updatedPatient.setPharmacy(pharmacyField.getText());
				    updatedPatient.setInsurance(insuranceField.getText());
				    updatedPatient.setAllergies(allergyArea.getText());
					
					if (rdbtnYes.isSelected())
					{
						updatedPatient.suspend();
					}
					else
					{
						updatedPatient.unSuspend();
					}
					
					if (!presenter.updateUser(updatedPatient))
					{
						JOptionPane.showMessageDialog(null, "User to update doesn't exist", 
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						patientModel.clear();

						for (Patient e : presenter.getPatientList())
						{
							patientModel.addElement(e);
						}
					}
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "You must enter integers for age", 
							"Error", JOptionPane.ERROR_MESSAGE);			
				}
			}
		});
		
		//Set password button handler
		btnSetPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String id = ((Patient)patientList.getSelectedValue()).getId();
				String newPassword = JOptionPane.showInputDialog("Enter new password");
				presenter.setPassword(id, newPassword);
			}
		});
		
		//Add patient button handler
		btnAddPatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				JFrame newPatientFrame = new newPatientFrame(presenter, patientModel);
				newPatientFrame.setVisible(true);
			}
		});
		
		//Remove patient handler
		btnRemovePatient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (patientList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select the patient to remove", 
						"Error", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					String id = ((Patient) patientList.getSelectedValue()).getId();
					presenter.removeUser(id);
					
					patientModel.clear();

					for (Patient e : presenter.getPatientList())
					{
						patientModel.addElement(e);
					}
				}
			}
		});
		
		JPanel nurses_panel = new JPanel();
		tabbedPane.addTab("Nurses", null, nurses_panel, null);
		nurses_panel.setLayout(null);
		
		JLabel lblId1 = new JLabel("ID:");
		lblId1.setBounds(264, 12, 42, 19);
		nurses_panel.add(lblId1);
		
		idField1 = new JTextField();
		idField1.setEditable(false);
		idField1.setBounds(343, 12, 114, 20);
		nurses_panel.add(idField1);
		idField1.setColumns(10);
		
		JLabel lblPw1 = new JLabel("PW:");
		lblPw1.setBounds(264, 42, 46, 14);
		nurses_panel.add(lblPw1);
		
		JLabel lblFirstName1 = new JLabel("First Name:");
		lblFirstName1.setBounds(264, 73, 86, 14);
		nurses_panel.add(lblFirstName1);
		
		JLabel lblLastName1 = new JLabel("Last Name:");
		lblLastName1.setBounds(264, 105, 86, 14);
		nurses_panel.add(lblLastName1);
		
		JLabel lblAge1 = new JLabel("Age:");
		lblAge1.setBounds(264, 132, 46, 14);
		nurses_panel.add(lblAge1);
		
		JLabel lblGender1 = new JLabel("Gender:");
		lblGender1.setBounds(264, 163, 46, 14);
		nurses_panel.add(lblGender1);
		
		fNameField1 = new JTextField();
		fNameField1.setEditable(false);
		fNameField1.setBounds(343, 72, 114, 20);
		nurses_panel.add(fNameField1);
		fNameField1.setColumns(10);
		
		lNameField1 = new JTextField();
		lNameField1.setEditable(false);
		lNameField1.setBounds(343, 102, 114, 20);
		nurses_panel.add(lNameField1);
		lNameField1.setColumns(10);
		
		ageField1 = new JTextField();
		ageField1.setEditable(false);
		ageField1.setBounds(343, 132, 114, 20);
		nurses_panel.add(ageField1);
		ageField1.setColumns(10);
		
		JLabel lblAddress1 = new JLabel("Address:");
		lblAddress1.setBounds(264, 195, 46, 14);
		nurses_panel.add(lblAddress1);
		
		JLabel lblPhone1 = new JLabel("Phone:");
		lblPhone1.setBounds(264, 251, 46, 14);
		nurses_panel.add(lblPhone1);
		
		phoneField1 = new JTextField();
		phoneField1.setEditable(false);
		phoneField1.setBounds(343, 252, 114, 20);
		nurses_panel.add(phoneField1);
		phoneField1.setColumns(10);
		
		JLabel lblEmail1 = new JLabel("E-mail:");
		lblEmail1.setBounds(264, 285, 46, 14);
		nurses_panel.add(lblEmail1);
		
		emailField1 = new JTextField();
		emailField1.setEditable(false);
		emailField1.setBounds(343, 282, 114, 20);
		nurses_panel.add(emailField1);
		emailField1.setColumns(10);
		
		JLabel lblMessages1 = new JLabel("Messages:");
		lblMessages1.setBounds(264, 315, 79, 14);
		nurses_panel.add(lblMessages1);
		
		btnShowMessages1 = new JButton("Show Messages");
		btnShowMessages1.setEnabled(false);
		btnShowMessages1.setBounds(343, 312, 114, 20);
		nurses_panel.add(btnShowMessages1);
		
		JLabel lblSuspended1 = new JLabel("Suspended:");
		lblSuspended1.setBounds(264, 354, 68, 14);
		nurses_panel.add(lblSuspended1);
		
		JScrollPane scrollPane_12 = new JScrollPane();
		scrollPane_12.setBounds(10, 11, 234, 398);
		nurses_panel.add(scrollPane_12);
		
		nurseModel = new DefaultListModel();
		for (Nurse e : presenter.getNurseList())
		{
			nurseModel.addElement(e);
		}
		
		nurseList = new JList(nurseModel);
		nurseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane_12.setViewportView(nurseList);
		
		btnSetPassword1 = new JButton("Set Password");
		btnSetPassword1.setEnabled(false);
		btnSetPassword1.setBounds(343, 42, 114, 20);
		nurses_panel.add(btnSetPassword1);
		
		btnAddNurse = new JButton("Add Nurse");
		btnAddNurse.setBounds(10, 430, 107, 27);
		nurses_panel.add(btnAddNurse);
		
		btnRemoveNurse = new JButton("Remove Nurse");
		btnRemoveNurse.setEnabled(false);
		btnRemoveNurse.setBounds(121, 430, 123, 27);
		nurses_panel.add(btnRemoveNurse);
		
		btnUpdateNurse = new JButton("Update Nurse");
		btnUpdateNurse.setEnabled(false);
		btnUpdateNurse.setBounds(343, 430, 114, 27);
		nurses_panel.add(btnUpdateNurse);
		
		addressArea1 = new JTextArea();
		addressArea1.setBounds(343, 192, 114, 48);
		nurses_panel.add(addressArea1);
		addressArea1.setEditable(false);
		
		rdbtnMale1 = new JRadioButton("Male");
		rdbtnMale1.setEnabled(false);
		rdbtnMale1.setBounds(343, 160, 53, 23);
		nurses_panel.add(rdbtnMale1);
		
		rdbtnFemale1 = new JRadioButton("Female");
		rdbtnFemale1.setEnabled(false);
		rdbtnFemale1.setBounds(396, 160, 109, 23);
		nurses_panel.add(rdbtnFemale1);
		
		genderGroup1 = new ButtonGroup();
		genderGroup1.add(rdbtnMale1);
		genderGroup1.add(rdbtnFemale1);
		
		rdbtnYes1 = new JRadioButton("Yes");
		rdbtnYes1.setEnabled(false);
		rdbtnYes1.setBounds(343, 350, 53, 23);
		nurses_panel.add(rdbtnYes1);
		
		rdbtnNo1 = new JRadioButton("No");
		rdbtnNo1.setEnabled(false);
		rdbtnNo1.setBounds(396, 350, 53, 23);
		nurses_panel.add(rdbtnNo1);
		
		suspendedGroup1 = new ButtonGroup();
		suspendedGroup1.add(rdbtnYes1);
		suspendedGroup1.add(rdbtnNo1);
		
		//Listen to when user clicks nurse list
		nurseList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				//When nurse list is unfocused, clear all the fields
				if (nurseList.getSelectedIndex() == -1)
				{
					idField1.setText("");
					fNameField1.setText("");
				    lNameField1.setText("");	
				    ageField1.setText("");
				    rdbtnMale1.setEnabled(false);
				    rdbtnFemale1.setEnabled(false);
				    rdbtnYes1.setEnabled(false);
				    rdbtnNo1.setEnabled(false);
				    genderGroup1.clearSelection();
				    suspendedGroup1.clearSelection();
				    addressArea1.setText("");
				    phoneField1.setText("");
				    emailField1.setText("");
				    idField1.setEditable(false);
					fNameField1.setEditable(false);
				    lNameField1.setEditable(false);
				    ageField1.setEditable(false);
				    addressArea1.setEditable(false);
				    phoneField1.setEditable(false);
				    emailField1.setEditable(false);
				    				    
				    btnSetPassword1.setEnabled(false);
				    btnShowMessages1.setEnabled(false);
				    btnRemoveNurse.setEnabled(false);
				    btnUpdateNurse.setEnabled(false);
				}
				//When user clicks on nurse, update appropriate fields
				else
				{
					idField1.setText((((Nurse) nurseList.getSelectedValue()).getId()));
					fNameField1.setText((((Nurse) nurseList.getSelectedValue()).getFirstName()));
				    lNameField1.setText((((Nurse) nurseList.getSelectedValue()).getLastName()));
				    ageField1.setText(Integer.toString(((Nurse) nurseList.getSelectedValue()).getAge()));
				    
				    if (((Nurse) nurseList.getSelectedValue()).getGender() == 0)
				    {
				    	rdbtnMale1.setSelected(true);
				    }
				    else
				    {
				    	rdbtnFemale1.setSelected(true);
				    }

				    addressArea1.setText((((Nurse) nurseList.getSelectedValue()).getAddress()));
				    phoneField1.setText((((Nurse) nurseList.getSelectedValue()).getPhone()));
				    emailField1.setText((((Nurse) nurseList.getSelectedValue()).getEmail()));
				    
				    if (((Nurse) nurseList.getSelectedValue()).isSuspended())
				    {
				    	rdbtnYes1.setSelected(true);
				    }
				    else
				    {
				    	rdbtnNo1.setSelected(true);
				    }

					fNameField1.setEditable(true);
				    lNameField1.setEditable(true);
				    ageField1.setEditable(true);
				    rdbtnMale1.setEnabled(true);
				    rdbtnFemale1.setEnabled(true);
				    rdbtnYes1.setEnabled(true);
				    rdbtnNo1.setEnabled(true);
				    addressArea1.setEditable(true);
				    phoneField1.setEditable(true);
				    emailField1.setEditable(true);
				    btnSetPassword1.setEnabled(true);
				    btnShowMessages1.setEnabled(true);
				    btnRemoveNurse.setEnabled(true);
				    btnUpdateNurse.setEnabled(true);
				}
			}
		});
		
		//Update nurse button handler
		btnUpdateNurse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					int gender;
					if (rdbtnMale1.isSelected())
					{
						gender = 0;
					}
					else
					{
						gender = 1;
					}
					
					Nurse updatedNurse = new Nurse(idField1.getText(), null, fNameField1.getText(), lNameField1.getText(), 
							Integer.parseInt(ageField1.getText()), gender, addressArea1.getText(),
							phoneField1.getText(), emailField1.getText());
					if (rdbtnYes1.isSelected())
					{
						updatedNurse.suspend();
					}
					else
					{
						updatedNurse.unSuspend();
					}
					
					if (!presenter.updateUser(updatedNurse))
					{
						JOptionPane.showMessageDialog(null, "User to update doesn't exist", 
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						nurseModel.clear();

						for (Nurse e : presenter.getNurseList())
						{
							nurseModel.addElement(e);
						}
					}
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "You must enter integers for age", 
							"Error", JOptionPane.ERROR_MESSAGE);			
				}
			}
		});
		
		//Set password button handler
		btnSetPassword1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String id = ((Nurse)nurseList.getSelectedValue()).getId();
				String newPassword = JOptionPane.showInputDialog("Enter new password");
				presenter.setPassword(id, newPassword);
			}
		});
		
		//Add nurse button handler
		btnAddNurse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				JFrame newNurseFrame = new newNurseFrame(presenter, nurseModel);
				newNurseFrame.setVisible(true);
			}
		});
		
		//Remove nurse button handler
		btnRemoveNurse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (nurseList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select the nurse to remove", 
						"Error", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					String id = ((Nurse) nurseList.getSelectedValue()).getId();
					presenter.removeUser(id);
					
					nurseModel.clear();

					for (Nurse e : presenter.getNurseList())
					{
						nurseModel.addElement(e);
					}
				}
			}
		});
		
		JPanel doctors_panel = new JPanel();
		tabbedPane.addTab("Doctors", null, doctors_panel, null);
		doctors_panel.setLayout(null);
		
		JLabel lblId2 = new JLabel("ID:");
		lblId2.setBounds(264, 12, 42, 19);
		doctors_panel.add(lblId2);
		
		idField2 = new JTextField();
		idField2.setEditable(false);
		idField2.setBounds(343, 12, 114, 20);
		doctors_panel.add(idField2);
		idField2.setColumns(10);
		
		JLabel lblPw2 = new JLabel("PW:");
		lblPw2.setBounds(264, 42, 46, 14);
		doctors_panel.add(lblPw2);
		
		JLabel lblFirstName2 = new JLabel("First Name:");
		lblFirstName2.setBounds(264, 73, 86, 14);
		doctors_panel.add(lblFirstName2);
		
		JLabel lblLastName2 = new JLabel("Last Name:");
		lblLastName2.setBounds(264, 107, 86, 14);
		doctors_panel.add(lblLastName2);
		
		JLabel lblAge2 = new JLabel("Age:");
		lblAge2.setBounds(264, 132, 46, 14);
		doctors_panel.add(lblAge2);
		
		JLabel lblGender2 = new JLabel("Gender:");
		lblGender2.setBounds(264, 163, 46, 14);
		doctors_panel.add(lblGender2);
		
		fNameField2 = new JTextField();
		fNameField2.setEditable(false);
		fNameField2.setBounds(343, 73, 114, 20);
		doctors_panel.add(fNameField2);
		fNameField2.setColumns(10);
		
		lNameField2 = new JTextField();
		lNameField2.setEditable(false);
		lNameField2.setBounds(343, 104, 114, 20);
		doctors_panel.add(lNameField2);
		lNameField2.setColumns(10);
		
		ageField2 = new JTextField();
		ageField2.setEditable(false);
		ageField2.setBounds(343, 132, 114, 20);
		doctors_panel.add(ageField2);
		ageField2.setColumns(10);
		
		JLabel lblAddress2 = new JLabel("Address:");
		lblAddress2.setBounds(264, 195, 46, 14);
		doctors_panel.add(lblAddress2);
		
		JLabel lblPhone2 = new JLabel("Phone:");
		lblPhone2.setBounds(264, 251, 46, 14);
		doctors_panel.add(lblPhone2);
		
		phoneField2 = new JTextField();
		phoneField2.setEditable(false);
		phoneField2.setBounds(343, 251, 114, 20);
		doctors_panel.add(phoneField2);
		phoneField2.setColumns(10);
		
		JLabel lblEmail2 = new JLabel("E-mail:");
		lblEmail2.setBounds(264, 295, 46, 14);
		doctors_panel.add(lblEmail2);
		
		emailField2 = new JTextField();
		emailField2.setEditable(false);
		emailField2.setBounds(343, 295, 114, 20);
		doctors_panel.add(emailField2);
		emailField2.setColumns(10);
		
		JLabel lblMessages3 = new JLabel("Messages:");
		lblMessages3.setBounds(260, 333, 79, 14);
		doctors_panel.add(lblMessages3);
		
		btnShowMessages2 = new JButton("Show Messages");
		btnShowMessages2.setEnabled(false);
		btnShowMessages2.setBounds(343, 333, 114, 20);
		doctors_panel.add(btnShowMessages2);
		
		JLabel lblSuspended2 = new JLabel("Suspended:");
		lblSuspended2.setBounds(260, 392, 68, 14);
		doctors_panel.add(lblSuspended2);
		
		JScrollPane scrollPane_13 = new JScrollPane();
		scrollPane_13.setBounds(10, 11, 234, 395);
		doctors_panel.add(scrollPane_13);
		
		//initialize doctor list
		doctorModel = new DefaultListModel();
		for (Doctor e : presenter.getDoctorList())
		{
			doctorModel.addElement(e);
		}
		
		doctorList = new JList(doctorModel);
		doctorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane_13.setViewportView(doctorList);
		
		btnSetPassword2 = new JButton("Set Password");
		btnSetPassword2.setEnabled(false);
		btnSetPassword2.setBounds(343, 43, 114, 20);
		doctors_panel.add(btnSetPassword2);
		
		btnAddDoctor = new JButton("Add Doctor");
		btnAddDoctor.setBounds(10, 430, 101, 23);
		doctors_panel.add(btnAddDoctor);
		
		btnRemoveDoctor = new JButton("Remove Doctor");
		btnRemoveDoctor.setEnabled(false);
		btnRemoveDoctor.setBounds(121, 430, 114, 23);
		doctors_panel.add(btnRemoveDoctor);
		
		btnUpdateDoctor = new JButton("Update Doctor");
		btnUpdateDoctor.setEnabled(false);
		btnUpdateDoctor.setBounds(337, 430, 120, 23);
		doctors_panel.add(btnUpdateDoctor);
		
		addressArea2 = new JTextArea();
		addressArea2.setBounds(343, 194, 114, 48);
		doctors_panel.add(addressArea2);
		addressArea2.setEditable(false);
		
		rdbtnMale2 = new JRadioButton("Male");
		rdbtnMale2.setEnabled(false);
		rdbtnMale2.setBounds(343, 160, 53, 23);
		doctors_panel.add(rdbtnMale2);
		
		rdbtnFemale2 = new JRadioButton("Female");
		rdbtnFemale2.setEnabled(false);
		rdbtnFemale2.setBounds(396, 160, 109, 23);
		doctors_panel.add(rdbtnFemale2);
		
		genderGroup2 = new ButtonGroup();
		genderGroup2.add(rdbtnMale2);
		genderGroup2.add(rdbtnFemale2);
		
		rdbtnYes2 = new JRadioButton("Yes");
		rdbtnYes2.setEnabled(false);
		rdbtnYes2.setBounds(343, 388, 53, 23);
		doctors_panel.add(rdbtnYes2);
		
		rdbtnNo2 = new JRadioButton("No");
		rdbtnNo2.setEnabled(false);
		rdbtnNo2.setBounds(396, 388, 53, 23);
		doctors_panel.add(rdbtnNo2);
		
		suspendedGroup2 = new ButtonGroup();
		suspendedGroup2.add(rdbtnYes2);
		suspendedGroup2.add(rdbtnNo2);
		
		//Listen to when user clicks on doctor list
		doctorList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				//When doctor list is unfocused, clear all the fields
				if (doctorList.getSelectedIndex() == -1)
				{
					idField2.setText("");
					fNameField2.setText("");
				    lNameField2.setText("");	
				    ageField2.setText("");
				    rdbtnMale2.setEnabled(false);
				    rdbtnFemale2.setEnabled(false);
				    rdbtnYes2.setEnabled(false);
				    rdbtnNo2.setEnabled(false);
				    genderGroup2.clearSelection();
				    suspendedGroup2.clearSelection();
				    addressArea2.setText("");
				    phoneField2.setText("");
				    emailField2.setText("");
				    idField2.setEditable(false);
					fNameField2.setEditable(false);
				    lNameField2.setEditable(false);
				    ageField2.setEditable(false);
				    addressArea2.setEditable(false);
				    phoneField2.setEditable(false);
				    emailField2.setEditable(false);

				    btnSetPassword2.setEnabled(false);
				    btnShowMessages2.setEnabled(false);
				    btnRemoveDoctor.setEnabled(false);
				    btnUpdateDoctor.setEnabled(false);
				}
				//When user click on doctor, updates appropriate fields
				else
				{
					idField2.setText((((Doctor) doctorList.getSelectedValue()).getId()));
					fNameField2.setText((((Doctor) doctorList.getSelectedValue()).getFirstName()));
				    lNameField2.setText((((Doctor) doctorList.getSelectedValue()).getLastName()));
				    ageField2.setText(Integer.toString(((Doctor) doctorList.getSelectedValue()).getAge()));
				    
				    if (((Doctor) doctorList.getSelectedValue()).getGender() == 0)
				    {
				    	rdbtnMale2.setSelected(true);
				    }
				    else
				    {
				    	rdbtnFemale2.setSelected(true);
				    }

				    addressArea2.setText((((Doctor) doctorList.getSelectedValue()).getAddress()));
				    phoneField2.setText((((Doctor) doctorList.getSelectedValue()).getPhone()));
				    emailField2.setText((((Doctor) doctorList.getSelectedValue()).getEmail()));
				    
				    if (((Doctor) doctorList.getSelectedValue()).isSuspended())
				    {
				    	rdbtnYes2.setSelected(true);
				    }
				    else
				    {
				    	rdbtnNo2.setSelected(true);
				    }

					fNameField2.setEditable(true);
				    lNameField2.setEditable(true);
				    ageField2.setEditable(true);
				    rdbtnMale2.setEnabled(true);
				    rdbtnFemale2.setEnabled(true);
				    rdbtnYes2.setEnabled(true);
				    rdbtnNo2.setEnabled(true);
				    addressArea2.setEditable(true);
				    phoneField2.setEditable(true);
				    emailField2.setEditable(true);

				    btnSetPassword2.setEnabled(true);
				    btnShowMessages2.setEnabled(true);
				    btnRemoveDoctor.setEnabled(true);
				    btnUpdateDoctor.setEnabled(true);
				}
			}
		});
		
		//Update Doctor button handler
		btnUpdateDoctor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					int gender;
					if (rdbtnMale2.isSelected())
					{
						gender = 0;
					}
					else
					{
						gender = 1;
					}
					
					Doctor updatedDoctor = new Doctor(idField2.getText(), null, fNameField2.getText(), lNameField2.getText(), 
							Integer.parseInt(ageField2.getText()), gender, addressArea2.getText(),
							phoneField1.getText(), emailField1.getText());
					if (rdbtnYes2.isSelected())
					{
						updatedDoctor.suspend();
					}
					else
					{
						updatedDoctor.unSuspend();
					}
					
					if (!presenter.updateUser(updatedDoctor))
					{
						JOptionPane.showMessageDialog(null, "User to update doesn't exist", 
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						doctorModel.clear();

						for (Doctor e : presenter.getDoctorList())
						{
							doctorModel.addElement(e);
						}
					}
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "You must enter integers for age", 
							"Error", JOptionPane.ERROR_MESSAGE);			
				}
			}
		});
		
		//Listen to when user clicks Treatment Record list
		TRlist.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) 
			{
				if (patientList.getSelectedIndex() == -1)
				{
					btnViewEditTreatmentRecords.setEnabled(false);
				}
				else
				{
					btnViewEditTreatmentRecords.setEnabled(true);
				}
			}
		});
		
		//ViewEditTreatmentRecord button handler
		btnViewEditTreatmentRecords.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String id = ((Patient) patientList.getSelectedValue()).getId();
				JFrame editTreatmentRecordFrame = new editTreatmentRecordFrame(presenter, id, (TreatmentRecord) TRlist.getSelectedValue(), trModel);
				editTreatmentRecordFrame.setVisible(true);
			}
		});
		
		//Set password button handler
		btnSetPassword2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String id = ((Doctor)doctorList.getSelectedValue()).getId();
				String newPassword = JOptionPane.showInputDialog("Enter new password");
				presenter.setPassword(id, newPassword);
			}
		});
		
		//Add doctor button handler
		btnAddDoctor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				JFrame newDoctorFrame = new newDoctorFrame(presenter, doctorModel);
				newDoctorFrame.setVisible(true);
			}
		});
		
		//remove doctor button handler
		btnRemoveDoctor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (doctorList.getSelectedIndex() == -1)
				{
					JOptionPane.showMessageDialog(null, "You must select the doctor to remove", 
						"Error", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					String id = ((Doctor) doctorList.getSelectedValue()).getId();
					presenter.removeUser(id);
					
					doctorModel.clear();

					for (Doctor e : presenter.getDoctorList())
					{
						doctorModel.addElement(e);
					}
				}
			}
		});
	}
}